package com.service.business.member.controller;

import com.service.business.framework.log.RequestLog;
import com.service.business.member.controller.rep.MemberAddressRep;
import com.service.business.member.controller.req.MemberAddressCreateReq;
import com.service.business.member.controller.req.MemberAddressUpdateReq;
import com.service.business.member.service.MemberAddressService;
import com.service.business.support.SessionUtil;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.OneResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("member-address")
public class MemberAddressController extends BaseController {
    @Autowired
    private MemberAddressService memberAddressService;

    @Autowired
    private SessionUtil sessionUtil;

    @PostMapping("/create")
    public BaseResponse create(@RequestBody @Validated MemberAddressCreateReq memberAddressCreateReq) {
        memberAddressService.addMemberAddress(sessionUtil.getUser().getUserId(), memberAddressCreateReq);
        return successSaveBaseResponse();
    }

    @RequestLog("会员地址列表")
    @GetMapping("/list")
    public OneResponse<List<MemberAddressRep>> getMemberAddress() {
        return successQueryOneResponse(memberAddressService.getMemberAddress(sessionUtil.getUser().getUserId()));
    }

    @PostMapping("/update/{addressId}")
    public BaseResponse updateMemberAddress(@PathVariable Integer addressId,
                                            @RequestBody @Validated MemberAddressUpdateReq memberAddressUpdateReq) {
        memberAddressService.updateAddress(addressId, memberAddressUpdateReq);
        return successUpdateBaseResponse();
    }

    @PostMapping("/delete/{addressId}")
    public OneResponse<Integer> delete(@PathVariable Integer addressId) {
        return successQueryOneResponse(memberAddressService.delete(addressId));
    }

    @PostMapping("/detail/{addressId}")
    public OneResponse<MemberAddressRep> getAddressDetail(@PathVariable Integer addressId) {
        return successQueryOneResponse(memberAddressService.getAddressDetail(addressId));
    }

    @PostMapping("/default-address/{addressId}")
    public OneResponse<Integer> changeDefaultAddress(@PathVariable Integer addressId) {
        memberAddressService.changeDefaultAddress(addressId);
        return successQueryOneResponse(addressId);
    }

    @GetMapping("/get-default-address")
    public OneResponse<MemberAddressRep> getDefaultAddress() {
        return successQueryOneResponse(memberAddressService.getMemberDefaultAddress(sessionUtil.getUser().getUserId()));
    }


}
